<html>
    <head>
        <style>
            body {
                padding: 0;
                margin: 0;
            }

            #position {
                width: 1px;
                height: 200px;
            }

            #scroller {
                width: 200px;
                height: 200px;
                overflow: scroll;
                margin-left: 100px;
            }

            #sticky {
                position: sticky;
                top: 0;
                left: 0;
                width: 100%;
                height: 50px;
                background: #0088ff;
                display: flex;
                justify-content: space-between;
            }

            .child {
                width: 50px;
                height: 50px;
                background: #00cc88;
            }

            #content {
                width: 100%;
                height: 400px;
            }

            #trigger-overflow {
                width: 1px;
                height: 1px;
                position: absolute;
                top: 2000px;
                left: 2000px;
            }

            [data-layout-correct="false"] {
                background: #dd1144 !important;
                opacity: 0.5;
            }
        </style>
    </head>
    <body>
        <div id="position"></div>
        <div id="scroller">
            <div id="sticky">
                <div class="child"></div>
            </div>
            <div id="content"></div>
        </div>
        <div id="trigger-overflow"></div>

        <script type="module" src="/src/imports/projection.js"></script>
        <script type="module" src="/src/imports/script-assert.js"></script>
        <script type="module" src="/src/imports/script-undo.js"></script>
        <script type="module">
            const { createNode } = window.Undo
            const {
                matchViewportBox,
                matchVisibility,
                matchOpacity,
                addPageScroll,
            } = window.Assert
            const { frame } = window.Projection

            const scroller = document.getElementById("scroller")
            const scrollerProjection = createNode(scroller, undefined, {
                layoutScroll: true,
            })

            const sticky = document.getElementById("sticky")
            const stickyProjection = createNode(sticky, scrollerProjection)

            const child = document.querySelector(".child")
            const childProjection = createNode(child, stickyProjection, {
                layoutId: "child",
            })

            const childOrigin = child.getBoundingClientRect()

            stickyProjection.willUpdate()
            childProjection.willUpdate()

            const newChild = document.createElement("div")
            newChild.classList.add("child")
            sticky.appendChild(newChild)
            const newChildProjection = createNode(newChild, stickyProjection, {
                layoutId: "child",
            })

            scroller.scrollTo(0, 100)

            stickyProjection.root.didUpdate()

            setTimeout(() => {
                matchViewportBox(child, childOrigin)
                matchViewportBox(newChild, childOrigin)
            }, 50)
        </script>
    </body>
</html>
